#!/bin/env python
#-*- coding:utf-8 -*-
#超市商品数据关联分析
author="将军"

import sys
import time
sys.path.append('modules/FPgrowtree')
import pandas
from modules.apriori import *
import FP_Grow_tree


class LoadData(object):

    def excelData(self,fname):
        '读取数据'
        #fname="excel文件名"
        data=pandas.read_excel(fname)
        return data

    def getCategorys(self,data):
        '获取类别'
        categorys=list(data.values[0])
            #类别
        return categorys

    def getCashierID(self,data):
        '获取收银员ID'
        cashier_id=list(data.index)[1:]
            #收银员ID
        return cashier_id

    def getBuyRecord(self,data):
        '获取用户购买记录'
        records=list(data.values[1:])
            #顾客消费记录
        return records

    def statistics(self,cashiers,records,categorys):
        '统计用户消费记录'
        #cashiers="收银员ID",records="用户购买商品记录",categorys="商品类别"
        result=[]
        for i in range(len(records)):
            rst=[cashiers[i]]
            for n in range(len(records[i])):
                if records[i][n]=='T':
                    rst.append(categorys[n])
                else:
                    rst.append(None)
            result.append(rst)
        return result


class AprioriForecast(object):
    #apriori算法预测关联性

    def changeData(self,fdata):
        '对数据进行映射,并转换数据'
        #fdata="数据框形式的数据"
        change=lambda x:pandas.Series(1,index=x[pandas.notnull(x)])
        mapok=map(change,fdata.as_matrix())
        data=pandas.DataFrame(list(mapok)).fillna(0)
        return data

    def forecast(self,data):
        '进行预测'
        support=0.2 #float(snum)
            #设置支持度阈值
        cfd=0.3 #float(cnum)
            #设置置信度阈值
        find_rule(data,support,cfd)
            #查看结果
        pass


class FPGrowthForecast(object):
    #fp-growth算法预测关联性

    def changeData(self,data):
        '转换数据,删除空项'
        for item in data:
            while None in item:
                item.remove(None)
        return data

    def forecast(self,data):
        '进行预测'
        surpport=10
            #最小支持度
        fp=FP_Grow_tree.FP_Grow_tree(data,[],surpport)        
        fp.printfrequent()
            #查看预测结果
        pass

if __name__=="__main__":
    ld=LoadData()
    af=AprioriForecast()
    fpgf=FPGrowthForecast()
    fname='data/supermarket_data_set.xls'
    data=ld.excelData(fname)
    records=ld.getBuyRecord(data)
    categorys=ld.getCategorys(data)
    cashiers=ld.getCashierID(data)
    rst=ld.statistics(cashiers,records,categorys)
    fdata=pandas.DataFrame(rst)
    #print('记录结果:',rst)
    #print('转换后的数据框:',fdata)
    #print(af.changeData(fdata))
    print('='*200)
    print('正在使用apriori算法进行预测...')
    print('='*200)
    af_s_time=time.time()
    afcdata=af.changeData(fdata)
    af.forecast(afcdata)
    af_e_time=time.time()
    print('#'*200)
    print('正在使用fp-growth算法进行预测...')
    print('#'*200)
    fp_s_time=time.time()
    fpcdata=fpgf.changeData(rst)
    fpgf.forecast(fpcdata)
    fp_e_time=time.time()
    aftime=int(af_e_time-af_s_time)*1000
    fptime=int(fp_e_time-fp_s_time)*1000
    if aftime<fptime:
        print('apriori算法效率更高,用时为:%s(ms)' %aftime)
        print('fp-growth算法用时:%s(ms)' %fptime)
        print('最小支持度阈值对效率的影响很大,这里耗时仅作参考')
    else:
        print('fp-growth算法效率更高,用时:%s(ms)' %fptime)
        print('apriori算法用时:%s(ms)' %aftime)
        print('最小支持度阈值对效率的影响很大,这里耗时仅作参考')
